import {getInfo, login, logout} from "@/api/business/login"
import {getToken, removeToken, setToken} from '@/utils/auth'

const user = {
    state: {
        token: getToken(),
        account: "",
        username: "",
        avatar: '',
        roles: [],
        permissions: [],
        unRead: {}//未读消息
    },
    mutations: {
        SET_UNREAD: (state, unRead) => {
            state.unRead = unRead
        },
        SET_TOKEN: (state, token) => {
            state.token = token
        },
        SET_ACCOUNT: (state, account) => {
            state.account = account
        },
        SET_AVATAR: (state, avatar) => {
            state.avatar = avatar
        },
        SET_USERNAME: (state, username) => {
            state.username = username
        },
        SET_ROLES: (state, roles) => {
            state.roles = roles
        },
        SET_PERMISSIONS: (state, permissions) => {
            state.permissions = permissions
        }
    },
    actions: {
        // 注册后登录
        registerAndLogin({commit}, token) {
            return new Promise((resolve, reject) => {
                setToken(token)
                commit('SET_TOKEN', token)
                resolve()
            })
        },
        // 登录
        Login({commit}, userInfo) {
            return new Promise((resolve, reject) => {
                login(userInfo).then(res => {
                    setToken(res.token)
                    commit('SET_TOKEN', res.token)
                    resolve()
                }).catch(error => {
                    reject(error)
                })
            })
        },
        // 获取用户信息
        GetInfo({commit, state}) {
            return new Promise((resolve, reject) => {
                getInfo().then(res => {
                    const user = res.user
                    if (res.roles && res.roles.length > 0) { // 验证返回的roles是否是一个非空数组
                        commit('SET_ROLES', res.roles)
                        commit('SET_PERMISSIONS', res.permissions)
                    } else {
                        commit('SET_ROLES', ['ROLE_DEFAULT'])
                    }
                    commit('SET_ACCOUNT', user.account)
                    commit('SET_AVATAR', user.avatar)
                    commit('SET_USERNAME', user.username)
                    resolve(res)
                }).catch(error => {
                    reject(error)
                })
            })
        },
        // 退出系统
        LogOut({commit, state}) {
            return new Promise((resolve, reject) => {
                logout().then(() => {
                    commit('SET_TOKEN', '')
                    commit('SET_AVATAR', '')
                    commit('SET_ACCOUNT', '')
                    commit('SET_USERNAME', '')
                    removeToken()
                    resolve()
                }).catch(error => {
                    reject(error)
                })
            })
        },
        // 前端 登出
        FedLogOut({commit}) {
            return new Promise(resolve => {
                commit('SET_TOKEN', '')
                removeToken()
                resolve()
            })
        }
    }
}

export default user
